package com.liuqianfei.mdurl;

import com.liuqianfei.util.StringUtil;

/**
 * @author liuqianfei
 */
public class Format
{
    public String format(MdUrl url)
    {
        String result = "";

        if (StringUtil.isNotEmpty(url.protocol))
            result += url.protocol;

        result += url.slashes ? "//" : "";

        if (StringUtil.isNotEmpty(url.auth))
            result += url.auth + "@";

        if (StringUtil.isNotEmpty(url.hostname) && url.hostname.indexOf(':') != -1)
            // ipv6 address
            result += '[' + url.hostname + ']';
        else
            if (StringUtil.isNotEmpty(url.hostname))
                result += url.hostname;

        if (StringUtil.isNotEmpty(url.port))
            result += ":" + url.port;

        if (StringUtil.isNotEmpty(url.pathname))
            result += url.pathname;

        if (StringUtil.isNotEmpty(url.search))
            result += url.search;

        if (StringUtil.isNotEmpty(url.hash))
            result += url.hash;

        return result;
    }

}
